from book import models
from rest_framework import serializers


def check_book(value):
    bok = models.Publish.objects.filter(name=value)
    if bok:
        raise serializers.ValidationError('出版社已存在')
    return value


class Bookseriaozers(serializers.ModelSerializer):
    publishname = serializers.SerializerMethodField(read_only=True)
    def get_publishname(self, obj):
        return obj.publish.name
    class Meta:
        model = models.Book
        fields = '__all__'

class Pubseriaozers(serializers.ModelSerializer):
    book_set=Bookseriaozers(many=True)

    class Meta:
        model = models.Publish
        fields = '__all__'
        extra_kwargs = {
                'name':{'required':True,'validators':[check_book],'error_messages':{'required':'出版社不能为空','max_length':'姓名不能超过10'}}
            }







